package com.bdqn.S64.service;

import com.bdqn.S64.pojo.TSysUser;
import com.bdqn.S64.util.SecurityUser;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SecurityUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        //查询出用户
        TSysUser user = userService.login(userName);
        //用户不存在抛出异常,框架自动处理
        if (user==null){
            throw new UsernameNotFoundException("该用户不存在!");
        }
        //用户存在将user写入到用户安全类里面
        SecurityUser securityUser=new SecurityUser(user);
        return securityUser;
    }
}
